package com.xnyzc.lhy.mis.service.impl.user.message;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.mis.mapper.user.message.OaSysMessageMapper;
import com.xnyzc.lhy.mis.service.user.message.IOaSysMessageService;
import com.xnyzc.lhy.order.entity.user.message.OaSysMessage;
import com.xnyzc.lhy.resource.entity.jpush.OaJpushConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Slf4j
@Service
public class OaSysMessageServiceImpl extends ServiceImpl<OaSysMessageMapper, OaSysMessage> implements IOaSysMessageService {
    @Autowired
    private OaSysMessageMapper oaSysMessageMapper;

    @Override
    public IPage<OaSysMessage> selectUserMessagePage(Long userId, Integer messageType, long current) {
        return null;
    }

    @Override
    public Rv saveMessage(OaSysMessage oaSysMessage) {

        if (CheckUtil.objIsEmpty(oaSysMessage.getPushCode())) {
            throw PangException.create(EErrorCode.missingArg);
        }

        if (CheckUtil.objIsEmpty(oaSysMessage.getDriverId())) {
            throw PangException.create(EErrorCode.missingArg);
        }

        Date now = new Date();
        log.info("------ 开始添加消息 ------");
        Map<String, String> map = oaSysMessageMapper.selectMessageTitle(oaSysMessage.getPushCode());
        oaSysMessage.setMessageId(IDUtil.nextId());
        oaSysMessage.setUserId(oaSysMessage.getDriverId());
        oaSysMessage.setUserType(1);
        oaSysMessage.setJumpMark(map.get(OaJpushConfig.JUMP_KEY));
        oaSysMessage.setCreateTime(now);
        oaSysMessage.setUpdateTime(now);
        Integer result = oaSysMessageMapper.insert(oaSysMessage);
        log.info("------ 结束添加消息 ------");
        return Rv.wrap(result);
    }
}
